﻿
namespace Sage.WindowsProcess.Events
{
    /// <summary>
    /// 服务级进程事件的基类
    /// </summary>
    /// <remarks>
    /// 初始化进程服务事件参数
    /// </remarks>
    /// <param name="processKey">进程键</param>
    public class ProcessServiceEventArgs(string processKey) : EventArgs
    {

        /// <summary>
        /// 进程唯一标识键
        /// </summary>
        public string ProcessKey { get; } = processKey ?? throw new ArgumentNullException(nameof(processKey));

        /// <summary>
        /// 事件时间戳
        /// </summary>
        public DateTime Timestamp { get; } = DateTime.Now;
    }

    /// <summary>
    /// 进程输出事件参数
    /// </summary>
    /// <remarks>
    /// 初始化进程输出事件参数
    /// </remarks>
    /// <param name="processKey">进程键</param>
    /// <param name="data">输出数据</param>
    public class ProcessServiceOutputEventArgs(string processKey, string data) : ProcessServiceEventArgs(processKey)
    {

        /// <summary>
        /// 输出数据
        /// </summary>
        public string Data { get; } = data ?? throw new ArgumentNullException(nameof(data));
    }

    /// <summary>
    /// 进程启动事件参数
    /// </summary>
    /// <remarks>
    /// 初始化进程启动事件参数
    /// </remarks>
    /// <param name="processKey">进程键</param>
    /// <param name="processId">进程ID</param>
    public class ProcessServiceStartedEventArgs(string processKey, int processId) : ProcessServiceEventArgs(processKey)
    {

        /// <summary>
        /// 进程ID
        /// </summary>
        public int ProcessId { get; } = processId;
    }

    /// <summary>
    /// 进程退出事件参数
    /// </summary>
    /// <remarks>
    /// 初始化进程退出事件参数
    /// </remarks>
    /// <param name="processKey">进程键</param>
    /// <param name="exitCode">退出代码</param>
    public class ProcessServiceExitedEventArgs(string processKey, int exitCode) : ProcessServiceEventArgs(processKey)
    {

        /// <summary>
        /// 退出代码
        /// </summary>
        public int ExitCode { get; } = exitCode;
    }

    /// <summary>
    /// 进程错误事件参数
    /// </summary>
    /// <remarks>
    /// 初始化进程错误事件参数
    /// </remarks>
    /// <param name="processKey">进程键</param>
    /// <param name="errorMessage">错误消息</param>
    /// <param name="exception">相关异常</param>
    public class ProcessServiceErrorEventArgs(string processKey, string errorMessage, Exception? exception = null) : ProcessServiceEventArgs(processKey)
    {

        /// <summary>
        /// 错误消息
        /// </summary>
        public string ErrorMessage { get; } = errorMessage ?? throw new ArgumentNullException(nameof(errorMessage));

        /// <summary>
        /// 相关异常
        /// </summary>
        public Exception? Exception { get; } = exception;
    }

    /// <summary>
    /// 进程状态更新事件参数
    /// </summary>
    /// <remarks>
    /// 初始化进程状态更新事件参数
    /// </remarks>
    /// <param name="processKey">进程键</param>
    /// <param name="cpuUsagePercent">CPU使用率(%)</param>
    /// <param name="memoryUsageBytes">内存使用量(字节)</param>
    /// <param name="runningTime">运行时间</param>
    /// <param name="threadCount">线程数</param>
    public class ProcessServiceStatusEventArgs(
        string processKey,
        double cpuUsagePercent,
        long memoryUsageBytes,
        TimeSpan runningTime,
        int threadCount) : ProcessServiceEventArgs(processKey)
    {

        /// <summary>
        /// CPU使用率(%)
        /// </summary>
        public double CpuUsagePercent { get; } = cpuUsagePercent;

        /// <summary>
        /// 内存使用量(字节)
        /// </summary>
        public long MemoryUsageBytes { get; } = memoryUsageBytes;

        /// <summary>
        /// 运行时间
        /// </summary>
        public TimeSpan RunningTime { get; } = runningTime;

        /// <summary>
        /// 线程数
        /// </summary>
        public int ThreadCount { get; } = threadCount;
    }
}
